const app = getApp();

Page({
  data: {
    elevatorFlag: 0,
    nameValue: '',
    phoneValue: '',
    region: ["省", "市", "区"],
    regionFlag: 1,
    textareaValue: '',
    floorValue: 0,
    remarksValue: '',
    addressStatus: 0,
    tname: '',
    tcontent: '',
    array: ['精华', '热门'],
    index: 0,
    userID: 0,
    tifc: '',
    images: [],
    upload: 0,
    photos: "",
  },

  bindPickerChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.data.tifc = this.data.array[e.detail.value]
    this.setData({
      index: e.detail.value
    })
  },

  getTitleValue: function (e) {
    this.data.tname = e.detail.value;
  },

  getTcontentValue: function (e) {
    this.data.tcontent = e.detail.value;
  },

  chooseImg() {
    let that = this;
    wx.chooseImage({
      count: 1, // 默认9
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success: function (res) {
        that.data.photos = res.tempFilePaths
        that.setData({
          imgUrls: [...(res.tempFilePaths || [])]
        });


      }
    });
  },

  onLoad: function () {
    let self = this;
    self.data.tifc = self.data.array[0]
    this.setData({
      userID: app.globalData.userID
    });
  },

  saveNewAddress: function () {
    var that = this;
 
    if (that.data.tname == '' || that.data.tcontent == '') {
      wx.showToast({
        title: '信息不完整',
      })
      return false;
    }

    wx.uploadFile({
      url: app.globalData.headurl + '/goods/upload', //仅为示例，非真实的接口地址
      filePath: that.data.photos[0],
      name: 'file',
      formData: {},
      success: function (res) {
        var filename = res.data;
        filename = filename.replace("\"", "").replace("\"", "");
        wx.request({
          url: app.globalData.headurl + '/lunt/addLunt',
          data: {
            tname: app.globalData.customer.cname,//这里获取名字失败就非常的迷。不过算了，本来就是无关紧要的功能。
            tlogo: 'https://img2.baidu.com/it/u=4226088621,3007262638&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500',
            tcontent: that.data.tcontent,
            topenid: app.globalData.openid,
            title: that.data.tname,
            img: filename
          },
          method: "POST",
          header: {
            "content-type": "application/x-www-form-urlencoded;charset=utf-8",
            //token: app.globalData.token
          },
          success: function (r) {
            wx.showToast({
              title: '发布成功',
            })
            wx.navigateBack({})
          }
        })
      }
    })




  }
});